package com.github.hiling.item.controller;

import com.alibaba.fastjson2.JSONObject;
import com.github.hiling.common.constant.ServiceNames;
import com.github.hiling.common.utils.HttpUtils;
import com.github.hiling.common.oauth.web.BaseController;
import com.github.hiling.common.oauth.web.UserInfo;
import lombok.Data;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.reactive.function.client.WebClient;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Author by hiling, Date on 11/20/2018.
 */
@RefreshScope
@RestController
@RequestMapping("/item")
public class ItemController extends BaseController {

    @Value("${aaa:default_aaa}")
    private String getAaa;

    @Value("${common.app}")
    private String getCommon;

    @GetMapping("config")
    public String getConfig() {
        return LocalDateTime.now().toString()+"-"+getAaa + " - "+ getCommon;
    }

    @Autowired
    private WebClient.Builder webClientBuilder;

    @Autowired
    private HttpUtils httpUtils;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 测试高可用
     *
     * @param request
     * @return
     */
    @GetMapping("url")
    public String get(HttpServletRequest request,String accessToken) {

        Boolean isLogin = isLogin();
        Long loginUserId = getUserId();
        UserInfo userInfo = getUserInfo();

        String url = "http://" + ServiceNames.AUTH_SERVICE + "/token?accessToken=" + accessToken;
        String jwtToken = httpUtils.get(url);

        return ">>>>>" + "Host:" + request.getRemoteHost()
                + "  Port: 【" + request.getServerPort()
                + "】 Path:" + request.getRequestURI()
                + " jwtToken:" + jwtToken
                + " userInfo:" + JSONObject.toJSONString(userInfo);
    }

    @GetMapping("users")
    public ResponseEntity<List<UserBean>> getUsers(HttpServletRequest request) {
        List<UserBean> users = new ArrayList<UserBean>();
        for (int i = 0; i < 10; i++) {
            users.add(new UserBean("name1_" + String.valueOf(i), i));
        }
        return new ResponseEntity<>(users,HttpStatus.OK);
    }

    @GetMapping("oom/{id}")
    public String testOOM(@PathVariable Integer id) {

        List<UserBean> users = new ArrayList<UserBean>();

        for (int i = 0; i < id; i++) {
            users.add(new UserBean("name_" + String.valueOf(i), i));
        }
        return "OK";
    }

    @Data
    public class UserBean {
        String name;
        int age;

        public UserBean(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }

    /**
     * 使用RabbitTemplate,这提供了接收/发送等等方法
     */
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return createTime;
    }

}
